//  Inside Microsoft Exchange 2007 Web Services
//  Chapter 6
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;
using ProxyHelpers.EWS;

namespace GetContactWithPhysicalAddresses
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            // TODO:  You must supply a valid ItemId for a contact in your call to 
            // GetContactWithPhysicalAddresses
            //ContactItemType contact = GetContactWithPhysicalAddresses(binding, contactId);
        }

        /// <summary>
        /// Retrieve a contact by Id and return two physical address properties (Listing 6-12).
        /// </summary>
        /// <param name="binding">Binding to use for the call</param>
        /// <param name="contactId">Id of contact to retrieve</param>
        /// <returns>The contact</returns>
        /// 
        public static ContactItemType GetContactWithPhysicalAddresses(
                                         ExchangeServiceBinding binding,
                                         ItemIdType contactId)
        {
            // Create our request
            //
            GetItemType request = new GetItemType();

            // Build our response shape using our handy overloaded constructor from Chapter 3.
            // Note that we are only interested in the two address property paths.
            //
            request.ItemShape = new ItemResponseShapeType(
                                       DefaultShapeNamesType.IdOnly,
                                       new PathToIndexedFieldType(
                                            DictionaryURIType.contactsPhysicalAddressCity,
                                            PhysicalAddressKeyType.Home.ToString()),
                                       new PathToIndexedFieldType(
                                            DictionaryURIType.contactsPhysicalAddressState,
                                            PhysicalAddressKeyType.Home.ToString()));
            request.ItemIds = new BaseItemIdType[] { contactId };

            // Make the request
            //
            GetItemResponseType response = binding.GetItem(request);
            ItemInfoResponseMessageType responseMessage =
                    binding.GetItem(request).ResponseMessages.Items[0] as
                            ItemInfoResponseMessageType;

            if (responseMessage.ResponseCode != ResponseCodeType.NoError)
            {
                throw new Exception("GetItem failed with response code: " +
                        responseMessage.ResponseCode.ToString());
            }
            else
            {
                // Cast the item as a contact item.
                //
                return responseMessage.Items.Items[0] as ContactItemType;
            }
        }

    }
}
